<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@page import="db.Album"%>

<%@page import="java.util.Date"%>
<%@page import="javax.swing.text.DateFormatter"%>
<%@page import="java.text.SimpleDateFormat"%><html xmlns="http://www.w3.org/1999/xhtml">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="my" tagdir="/WEB-INF/tags" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import=" java.text.DateFormat"%>
<%@page import="db.Picture"%>
<%@page import="presentation.Get16ImagesBean"%>
<head>
<link rel="stylesheet" type="text/css" href="css/styleGallery.css" />
<title>Начало</title>  
</head>
<%  
	Long albumId=null;
      try{
    	  albumId=Long.parseLong(request.getParameter("album_id"));
	}catch( Exception e){  			
		albumId=null;
	};  
	Integer pageNum =null;
	 try{
			 pageNum = Integer.parseInt(request.getParameter("page"));
			 
	}catch( Exception e){  			
			pageNum=1;
	};  
	Boolean search=null;
	 try{
			 search = (Boolean)request.getAttribute("search");
			 if (search == null) {
				 search = false;
			 }
	}catch( Exception e){  			
			search = false;
	};  
	long allPicNum;
	Get16ImagesBean b ;
	List<Picture> pictures;
	if(search){
		  b = new Get16ImagesBean(pageNum,albumId);
		  pictures =(List<Picture>)session.getAttribute("findPictures");
		  long allNum=pictures.size();
		  if(allNum%16!=0){
			allPicNum=(allNum/16)+1;
		  }else{
			 allPicNum=(allNum/16);
		  }
	}else{
		 b = new Get16ImagesBean(pageNum,albumId);
		 pictures =b.getPictures();
		 allPicNum=b.getAllPicNum(albumId);
	}
%>
<body id="bd">
	<div id="container">
		<my:header/>
		<%if (session.getAttribute("user")==null){ %>
		<my:menulogout/>
		<div id="midcontainer"> 
		<%}else{ 
		 List<Album> albums= b.getUserAlbums( (Long)session.getAttribute("userid"));%>
		
	 
       		<my:menulogin/>
    <div id="midcontainer"> 
<%} 
	if (session.getAttribute("userid") != null && albumId != null) { 
%>
		
		<table align="center">
   			<tr><td width="85%">
   		
   		<div id="uploadsite">
		<form name="myform" action="UploadServlet" method="post" enctype="multipart/form-data" id="form1">
			<div id="labelupl">Прикачване на снимки</div>
			<table>
			
			
			<input type="hidden" name="albumid" value="<%=albumId %>" />
		<tr>
			<td align="left">
			Име:
			</td>
			<td align="left">
			<%
				Date date = new Date(System.currentTimeMillis());
				SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy" );
				
			%>
			<input class="box" type="text" name="title" value="<%=sdf.format(date) %>"/>
			</td>
			</tr>
			<tr>
			<td align="left">
			Снимка:
			</td>
			<td>
			<input class="box" name="user_file" type="file" value="Избери" />
			</td>
			</tr>
			</table>
			<div id="labelupl"><input style="float: none" class="button1" type="submit" value="Добави" /></div>
		</form>
	</div>
   		
   		</td></tr>
   		</table>
   		
		<%
	}
		if(pictures.size()==0){ %>
		
			<%if (search) { %>
				<p id="err">Няма намерени снимки!</p>
			<%} else { %>
	   			 <p id="err">Няма качени до момента снимки!</p>
	  		<%} %>
			<%}else{ %>
   		<br>
   		
	<table align="center">
   		<%
   		application.setAttribute("pictures", pictures);
   		application.setAttribute("page",pageNum);
   		session.setAttribute("pageNum",pageNum);
   		session.setAttribute("allPicNum",allPicNum);
   		%>
   		
   		<%
   		if (pictures==null) {
   			pictures = new ArrayList<Picture>();
   		}else{
   		
   		for (int i=0; i<pictures.size(); i++){
   			if((i==0)&&(pageNum==1)){
   				session.setAttribute("startPic", pictures.get(i).getPictureId());
   			}
   			if( (pageNum==allPicNum)&&(i==(pictures.size()-1))){
   				session.setAttribute("endPic", pictures.get(i).getPictureId());
   			}
   		  if(i%4==0){%>
   		   <tr></tr>
   		 <%};%>
   		
     			<td ><div id ="pic">
         		  <div class="shadow">
         		  <a href="ViewPicture.jsp?image=<%=pictures.get(i).getPictureId()%>"><img  src="Image?image=<%=pictures.get(i).getPictureId()%>&size=s"/> </a>		
          		  </div>
          		  <div id="pictext">
          		  <li><%=pictures.get(i).getPictureTitle()%></li><br>
	           
    	          </div>
     		   </div>
     		   </td>
     		   <%}; %>
   
         </table>
        <%if(albumId!=null){ %>
          <div id="page">
        	  <%if ((pageNum==1)&&(allPicNum>1)){ %>
        		 <li> <img width="50" height="50" alt="Предишна" src="img/prev.png" /> &nbsp;</li>		
   				<li> <a class="number" href="Home.jsp?album_id=<%=albumId %>&page=<%=++pageNum%>">&nbsp; <img width="50" height="50" alt="Следваща" src="img/next.png" /><a></li>
        	 <%}else if ((pageNum==1)&&(allPicNum>1)){ %>
        		 <li> <a class="number" href="Home.jsp?album_id=<%=albumId %>&page=<%=--pageNum%>"> <img width="50" height="50" alt="Предишна" src="img/prev.png" /> &nbsp;<a></li>		
   				<li>&nbsp; <img width="50" height="50" alt="Следваща" src="img/next.png" /></li>
         	 <%}else if(( pageNum==allPicNum )&&(allPicNum==1)){
        	 %>
       			 <li> <img width="50" height="50" alt="Предишна" src="img/prev.png" /> &nbsp;</li>		
   				<li>&nbsp; <img width="50" height="50" alt="Следваща" src="img/next.png" /></li>
         	<%}else{ %>
  	    		<li> <a class="number" href="Home.jsp?album_id=<%=albumId %>&page=<%=--pageNum%>">&nbsp; <img width="30" height="30" alt="Предишна" src="img/prev.png" /><a></li>	
  	    		<%pageNum++;%>	
   				<li> <a class="number" href="Home.jsp?album_id=<%=albumId %>&page=<%=++pageNum%>">&nbsp; <a></li>
   			<%} %>
         </div>
       <%}else{ %>
         <div id="page">
         <%if ((pageNum==1)&&(allPicNum>1)){ %>
         <li> <img width="50" height="50" alt="Предишна" src="img/prev.png" />&nbsp;</li>		
   		<li> <a class="number" href="Home.jsp?page=<%=++pageNum%>">&nbsp; <img width="50" height="50" alt="Следваща" src="img/next.png" /><a></li>
         <%}else if(( pageNum==allPicNum )&&(allPicNum!=1)){
        	 %>
         <li> <a class="number" href="Home.jsp?page=<%=--pageNum%>"> <img  width="50" height="50" alt="Предишна" src="img/prev.png" /> &nbsp;<a></li>		
   		<li>&nbsp; <img width="50" height="50" alt="Следваща" src="img/next.png" /></li>
         <%}else if(( pageNum==allPicNum )&&(allPicNum==1)){
        	 %>
        <li>  <img width="50" height="50" alt="Предишна" src="img/prev.png" />&nbsp;</li>		
   		<li>&nbsp;<img width="50" height="50" alt="Следваща" src="img/next.png" /> </li>
         <%}else{ %>
  	    <li> <a class="number" href="Home.jsp?page=<%=--pageNum%>">&nbsp; <img width="50" height="50" alt="Предишна" src="img/prev.png" /> <a></li>	
  	    <%pageNum++;%>	
   		<li> <a class="number" href="Home.jsp?page=<%=++pageNum%>">&nbsp; <img width="50" height="50" alt="Следваща" src="img/next.png" /><a></li>
   		<%} %>
    </div>
    <%} %><!-- else na albumId!=null -->
    <%} %><!-- else na picture!=null -->
   		<%} %>	
</div><!-- midcontainer -->

<%b.releaseResources(); %>
<my:footer/>
</body>
</html>